package com.hspedu.furns.web;

import com.hspedu.furns.entity.Cart;
import com.hspedu.furns.entity.Member;
import com.hspedu.furns.entity.Order;
import com.hspedu.furns.entity.OrderItem;
import com.hspedu.furns.service.OrderService;
import com.hspedu.furns.service.impl.OrderServiceImpl;
import com.hspedu.furns.utils.JDBCUtilsByDruid;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @author 徐
 * @version 1.0
 */
public class OrderServlet extends BasicServlet {
    //定义OrderService对象
    private OrderService orderService = new OrderServiceImpl();

    /**
     * 接收session中的购物车生成订单信息
     *
     * @param request
     * @param response
     */
    protected void saveOrder(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //1.获取cart对象
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        //判断cart是否为null,即判断购物车中是否有商品
        // cart.isEmpty() 表示有购物车在session，但是没有家居数据
        if (cart == null || cart.isEmpty()) {//没有登录，使其添加商品
            //请求转发到首页
            request.getRequestDispatcher("/index.jsp").forward(request, response);
            return;
        }
        //2.获取登录的memberId
        Member member = (Member) request.getSession().getAttribute("member");
        //判断member是否为空，即用户是否登录过
        if (member == null) {//没有登录，使其登录
            //请求转发到登录页面
            request.getRequestDispatcher("/views/member/login.jsp")
                    .forward(request, response);
            return;//直接返回
        }

        //满足以上条件后
        String orderId = orderService.saveOrder(cart, member.getId());
        //就可以生成订单
        request.getSession().setAttribute("orderId", orderId);

        //分析
        //1. 如果我们只是希望对orderService.saveOrder()进行事务控制
        //2. 我们可以不使用过滤器, 我们直接在这个位置，进行提交和回滚即可
//        String orderId = null;
//        try {
//            orderId = orderService.saveOrder(cart, member.getId());
//            JDBCUtilsByDruid.commit();//提交
//        } catch (Exception e) {
//            JDBCUtilsByDruid.rollback();
//            e.printStackTrace();//发生异常回滚
//        }

        //重定向到check.jsp
        response.sendRedirect(request.getContextPath() + "/views/order/checkout.jsp");

    }

    /**
     * 1.接收订单id
     * 2.根据id查询订单信息，并返回
     *
     * @param request
     * @param response
     */
    public void showOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从session中取出订单号
        String orderId = (String) request.getSession().getAttribute("orderId");
        //根据订单id,返回order对象
        Order order = orderService.showOrder(orderId);
        //将 order 放入session域中
        request.getSession().setAttribute("order", order);
        //转发到order.jsp页面
        request.getRequestDispatcher("/views/order/order.jsp").forward(request, response);

    }

    /**
     * 根据订单id查询详细记录
     * 即每个家居的信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void showOrderItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从session中取出订单号
        String orderId = (String) request.getSession().getAttribute("orderId");
        //根据 id 返回返回 orderItem 对象
        List<OrderItem> orderItems = orderService.showOrderItem(orderId);

        //统计购物车所有家具数量
        //前面生成订单清空了购物车，不能用cart对象取出
        int allCount = 0;
        for (OrderItem orderItem : orderItems) {
            allCount += orderItem.getCount();
        }
        //把 allCount 放到 session 域中
        request.getSession().setAttribute("allCount", allCount);

        //把orderItems放到session域中
        request.getSession().setAttribute("orderItems", orderItems);
        //转发到order_detail.jsp页面
        request.getRequestDispatcher("/views/order/order_detail.jsp").forward(request, response);
    }

    /**
     * 查询所有订单信息
     * 用于订单管理进行展示
     * @param request
     * @param response
     */
    public void showOrders(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //System.out.println("OrdrServlet中的showOrders被调用...");
        List<Order> orders = orderService.queryOrders();
        //把orders放入到request域中去
        request.setAttribute("orders",orders);
        //请求转发
        request.getRequestDispatcher("/views/manage/furn_order.jsp").
                forward(request,response);
    }

    /**
     * 订单管理中查看详情
     * 根据订单id进行查询
     * @param request
     * @param response
     */
    public void showOrderDetails(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //获取url带的参数id
        String orderId = request.getParameter("orderId");
        //根据订单id查询订单详情
        List<OrderItem> orderDetails = orderService.showOrderItem(orderId);
        //将orderDetails放入到request域中
        request.setAttribute("orderDetails",orderDetails);
        //请求转发
        request.getRequestDispatcher("/views/manage/furn_orderDetail.jsp").
                forward(request,response);
    }



}
